const router = require('express').Router()
const R = require('ramda')
const Node = require('../models/Node')
const U = require('../utils')

function not_nil(obj){
  return R.not(R.isNil(obj))
}

function put_params_exsit(req,res,next){
  const body = req.body
  console.log(body)
  if(R.and(body.title != null,body.isArticle != null)){
    next()
    return
  }
  res.json(U.err('错误,请提供标题和是否为文章'))
}

router.param('id',(req,res,next,id) => {
  req.node_id = id
  next()
})

router.route('/:id')
 .get((req,res,next) => {
   console.log('get nodes by pid: ' + req.node_id)
   Node.list_by_pid(req.node_id).then(nodes => {
     res.json(U.ok(nodes))
   })
 })
 .put(put_params_exsit,(req,res,next) => {
   console.log('put nodes by pid' + req.node_id)
   Node.add_under(req.node_id,req.body.title,req.body.isArticle).then(result => {
     res.json(U.ok('添加成功'))
   })
 })


module.exports = router